/**
 * 
 */
package com.zt.samples.license;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;

/**
 * @author yinxb
 *
 */
public class KeyGenerater {
	/**
	 * 私钥.
	 */
	private byte[] priKey;
	/**
	 * 公钥.
	 */
	private byte[] pubKey;
	/**
	 * 产生公钥和私钥.
	 */
	public void generater() {
		try {
			KeyPairGenerator keygen = KeyPairGenerator .getInstance("RSA");
			SecureRandom secrand = new SecureRandom();
			secrand.setSeed("www.zooltech.com".getBytes()); //初始化随机生成器
			keygen.initialize(1024, secrand);
			KeyPair keys = keygen.genKeyPair();
			PublicKey pubkey = keys.getPublic();
			PrivateKey prikey = keys.getPrivate();
			pubKey = Base64.encodeToByte(pubkey.getEncoded());
			priKey = Base64.encodeToByte(prikey.getEncoded());
			System.out.println("\nencoding: "+pubkey.getFormat());
			System.out.println("\npubKey = " + new String(pubKey));
			System.out.println("\npriKey = " + new String(priKey));
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args){
		new KeyGenerater().generater();
	}
}
